<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="formData" label-width="120px" style="margin-left: 120px; margin-top:30px" :model="formData" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="formData.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateUser">更新</el-button>
                <el-button @click="$router.back()">返回</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 直接引入组件，用标签形式使用 -->
            <!-- <UserInfo /> -->
            <!-- 也可以用动态组件 -->
            <component :is="componentName" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <JobUser />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import UserInfo from './components/user-info'
import JobUser from './components/job-info'
export default {
  components: {
    UserInfo, JobUser
  },
  data() {
    return {
      componentName: 'UserInfo',
      formData: {
        username: '',
        // password: '',
        // 由于密码是乱码，所以先动态绑定一个空变量，更新数据时，再把动态绑定的数据赋值给password
        password2: ''
      },
      rules: {
        username: [
          { required: true, message: '名字不能为空', trigger: 'blur' },
          { min: 1, max: 6, message: '不能超过 1-6 位', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '不能超过 6-16 位', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    // 获取员工详情数据
    async getUserDetailById() {
      // console.log(this.$route)
      this.formData = await getUserDetailById(this.$route.params.id)
    },
    // 更新员工基本信息
    async updateUser() {
      // 校验表单
      this.$refs.formData.validate()
      // 发请求
      await saveUserDetailById({ ...this.formData, password: this.formData.password2 })
      // 提示成功
      this.$message.success('更新成功')
    }
  }
}
</script>

<style>

</style>
